package com.hongdee.insurance.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.StringUtil;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.insurance.Constant;
import com.hongdee.insurance.order.entity.TInsuranceOrder;
import com.hongdee.insurance.order.service.ITInsuranceOrderService;
import com.hongdee.insurance.order.service.ITInsuranceOrderStatsticsService;
import com.hongdee.insurance.order.service.ITInsuranceOrderToCrmService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/4/27.
 */
@RestController
@RequestMapping("/insurance/Order")
public class TInsuranceOrderController {
    @Autowired
    private ITInsuranceOrderService itInsuranceOrderService;
    @Autowired
    private ITInsuranceOrderStatsticsService orderStatsticsService;
    @Autowired
    private ITInsuranceOrderToCrmService orderToCrmService;
    /**
     * 保险出单
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity Save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TInsuranceOrder data) throws Exception {
        EntityUtils.valid(data);
        data.setInsuranceType(String.valueOf(Constant.INSURNACE_TYPE.def.ordinal()));
        this.itInsuranceOrderService.save(data,uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * 保存客户求助内容
     * @param uid
     * @param token
     * @param helpId
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save/seek/help")
    @ResponseBody
    public ResultEntity saveSeekHelp(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "helpId", defaultValue = "") String helpId,
                                     @RequestBody TInsuranceOrder data) throws Exception {
        EntityUtils.valid(data);
        data.setInsuranceType(String.valueOf(Constant.INSURNACE_TYPE.def.ordinal()));
        this.itInsuranceOrderService.saveSeekHelpOrder(data,uid,helpId);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("出单成功，求助内容已完成!!");
        return res;
    }

    /**
     * 编辑保单
     * 1、编辑保单暂定只编辑保险项目、商业险、交强险合同号
     * 2、编辑时，如商业险或交强险已经结算 则系统不在做出修改
     * @param uid
     * @param token
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResultEntity update(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestBody Map data) throws Exception {
        ResultEntity res=new ResultEntity();
        itInsuranceOrderService.update(data,uid);
        res.setSuccess(true);
        res.setMsg("保单信息更新成功!!");
        return res;
    }

    /**
     * 获取保险单详情
     * @param uid
     * @param token
     * @param qId
     * @param carId
     * @return
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "qId", defaultValue = "") String qId,
                             @RequestParam(name = "carId", defaultValue = "") String carId) throws Exception {
        ResultEntity res=new ResultEntity();
        Map infoMap=this.itInsuranceOrderService.info(uid,qId,carId);
        res.setData(infoMap);
        res.setSuccess(true);
        return res;
    }

    @RequestMapping("/info/app")
    @ResponseBody
    public ResultEntity infoApp(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "qId", defaultValue = "") String qId,
                             @RequestParam(name = "carId", defaultValue = "") String carId) throws Exception {
        ResultEntity res=new ResultEntity();
        Map infoMap=this.itInsuranceOrderService.info(uid,qId,carId);
        infoMap.put("payType",Constant.getPayType(StringUtil.formatEmpty(infoMap.get("payType"))));
        res.setData(infoMap);
        res.setSuccess(true);
        return res;
    }

    /**
     * 获得保险相应信息用于编辑使用
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/infoToUpd")
    @ResponseBody
    public ResultEntity infoToUpd(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res=new ResultEntity();
        Map infoMap=this.itInsuranceOrderService.getInsuranceOrderToUpd(id,uid);
        res.setData(infoMap);
        res.setSuccess(true);
        return res;
    }

    /**
     * 保险单分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {
        Map map = itInsuranceOrderService.page(uid, start, limit, data,true);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 统计信息
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page/statistics")
    @ResponseBody
    public Map statistics(@RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token,
                          @RequestBody Map data) throws Exception {
        Map map=itInsuranceOrderService.orderPageStatistics(data,uid);
        map.put("success",true);
        return map;
    }

    /**
     * 已有保险单
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping("/hasOrder")
    @ResponseBody
    public Map hasOrder(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "carId", defaultValue = "50") String carId) throws Exception {
        Map data=new HashMap();
        data.put("carId",carId);
        Map map = itInsuranceOrderService.page(uid,start,limit,data,false);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
    /**
     * 保险单退回
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/returnOrder")
    @ResponseBody
    public ResultEntity returnOrder(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id",defaultValue = "")String id,
                             @RequestBody Map data) throws Exception {
        this.itInsuranceOrderService.insuranceReturnOrder(uid,data);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("退回成功！");
        return res;
    }

    /**
     * 保险单新增录入
     * @param uid
     * @param token
     * @param data
     * @return
     */
    @RequestMapping("/addInsuranceOrder")
    @ResponseBody
    public ResultEntity addInsuranceOrder(@RequestParam(name = "uid", defaultValue = "") String uid,
                                          @RequestParam(name = "token", defaultValue = "") String token,
                                          @RequestBody Map data) throws Exception {
        ResultEntity res=new ResultEntity();
        this.itInsuranceOrderService.addInsuranceOrder(data,uid);
        res.setMsg("保险单信息已成功录入！！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 提交保险进行审核
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/submit")
    @ResponseBody
    public ResultEntity subInsuranceOrder(@RequestParam(name = "uid", defaultValue = "") String uid,
                                          @RequestParam(name = "token", defaultValue = "") String token,
                                          @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res=new ResultEntity();
        this.itInsuranceOrderService.subInsuranceOrder(uid,id);
        res.setMsg("保险单信息已成功录入！！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 追加保单
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save/add")
    @ResponseBody
    public ResultEntity saveAdd(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestBody Map data) throws Exception {
        ResultEntity res=new ResultEntity();
        this.itInsuranceOrderService.insuranceOrderAppend(data,uid);
        res.setMsg("保险单信息已成功录入！！");
        res.setSuccess(true);
        return res;
    }


    /**
     * 返现确认
     * @param uid
     * @param token
     * @param id
     * @param time
     * @return
     * @throws Exception
     */
    @RequestMapping("/confimBack")
    @ResponseBody
    public ResultEntity confimBack(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id",defaultValue = "")String id,
                                   @RequestParam(name = "time",defaultValue = "")String time,
                                   @RequestParam(name = "backPrice",defaultValue = "0")String backPrice) throws Exception {
        ResultEntity res=new ResultEntity();
        this.itInsuranceOrderService.confimBack(id,uid,time,backPrice);
        res.setSuccess(true);
        res.setMsg("返现确认成功！");
        return res;
    }

    /**
     * 保险单退回
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/returnInsurance")
    @ResponseBody
    public ResultEntity returnInsurance(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        @RequestParam(name = "token", defaultValue = "") String token,
                                        @RequestParam(name = "id",defaultValue = "")String id) throws Exception {
        ResultEntity res=new ResultEntity();
        this.itInsuranceOrderService.returnInsurance(id,uid);
        res.setSuccess(true);
        res.setMsg("退单信息提交成功，待审核通过后进行退单！！");
        return res;
    }

    /**
     * 结算管理 明细增加保单查询
     * @param uid
     * @param token
     * @param billId
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/blanceInsuanceOrderList")
    @ResponseBody
    public Map blanceInsuanceOrderList(@RequestParam(name = "uid", defaultValue = "") String uid,
                                       @RequestParam(name = "token", defaultValue = "") String token,
                                       @RequestParam(name = "billId",defaultValue = "")String billId,
                                       @RequestParam(name = "start",defaultValue = "")String start,
                                       @RequestParam(name = "limit",defaultValue = "")String limit,
                                       @RequestBody Map data) throws Exception {
        Map map=itInsuranceOrderService.getBlanceInsuanceOrderList(data,uid,billId,start,limit);
        map.put("success",true);
        map.put("msg","查询成功");
        return map;
    }

    /**
     * 保险续保日台账导出Excel
     * @param uid
     * @param token
     * @param data
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/statics/oa/day")
    @ResponseBody
    public ResultEntity insuranceOAByDay(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestBody Map data,
                                HttpServletRequest request) throws Exception {
        String path=Constant.uploadPath(request);
        String fileId=Constant.copyExcelToNewPath(request,"xubaoribaotaizhang");
        XSSFWorkbook wb =orderStatsticsService.expInsuranceOrderToExcel(uid,data,"day",path,fileId);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;
    }

    /**
     * 续保OA台账
     * @param uid
     * @param token
     * @param data
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/statics/oa")
    @ResponseBody
    public ResultEntity insuranceOA(@RequestParam(name = "uid", defaultValue = "") String uid,
                                         @RequestParam(name = "token", defaultValue = "") String token,
                                         @RequestBody Map data,
                                         HttpServletRequest request) throws Exception {
        String path=Constant.uploadPath(request);
        String fileId=Constant.copyExcelToNewPath(request,"xubaooataizhang");
        XSSFWorkbook wb =orderStatsticsService.expInsuranceOrderToExcel(uid,data,"oa",path,fileId);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;
    }

    /**
     * 续保台账
     * @param request
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/statics/account")
    public ResultEntity insuranceAccount(HttpServletRequest request,@RequestParam(name = "uid", defaultValue = "") String uid,
                                         @RequestParam(name = "token", defaultValue = "") String token,
                                         @RequestBody Map data) throws Exception {

        String path=Constant.uploadPath(request);
        String fileId=Constant.copyExcelToNewPath(request,"xubaotaizhang");
        XSSFWorkbook wb=orderStatsticsService.insuranceAccountExpExcel(uid,data,path,fileId);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;
    }


}
